
#region Using Statements
using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

using Elephant.Utility;
using Elephant.Utility.Components;

using Elephant.Framework;
using Elephant.Framework.Services;

namespace SpinningPyramid
{

    /*
     * uncomment for another way of creating an entity
     * 
    public class Pyramid : Entity
    {
        public Pyramid()
            : base("A Pyramid", new Spinner(), new PyramidRenderer()) { }
    }
    */
    
    public class GameWindow : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        
        IContentService content;
        ISceneService scene;

        public GameWindow()
        {
            GameHelper.Game = this;

            graphics = new GraphicsDeviceManager(this);

            this.IsMouseVisible = true;
            this.Window.Title = String.Format("Elephant Samples: A Spinning Pyramid");
            
            // installing the major services
            Components.Add(new SceneComponent(this));
            Components.Add(new ContentProvider(this));

            // getting a reference to the services (could have just kept the references as we installed them)
            scene = Services.GetService(typeof(ISceneService)) as ISceneService;
            content = Services.GetService(typeof(IContentService)) as IContentService;
        }

        protected override void Initialize()
        {
            //
            // following shows different ways of creating entities
            //
            /*
            Entity camera = scene.Factory.Create<Entity>("A Camera", new LookAtCamera());
            camera.Find<Transform>().Position = new Vector3(0, 2, 15);
            camera.Find<LookAtCamera>().ClearColor = Color.Crimson;
            */
            scene.Factory.CreateFrom("Camera.xml");

            //scene.Factory.Create<Entity>("A Pyramid", new Spinner(), new PyramidRenderer());
            //scene.Factory.Create<Pyramid>();
            scene.Factory.CreateFrom("SpinningPyramid.xml");

            // not used
            scene.Factory.Create<Entity>("Dummy 1");
            scene.Factory.Create<Entity>("Dummy 2");

            base.Initialize();
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent) {
                content.Unload();
            }
        }

        KeyboardState ksLast = Keyboard.GetState();
        
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyUp(Keys.Delete) && ksLast.IsKeyDown(Keys.Delete)) {
                // remove the spinner component from out pyramid entity
                scene.Manager.Find("A Pyramid").Remove<Spinner>();

                // clear all entities, and the accompanied components
                //scene.Manager.Clear();
            }

            if (Keyboard.GetState().IsKeyUp(Keys.Insert) && ksLast.IsKeyDown(Keys.Insert)) {
                // add a spinner component, to our pyramid entity
                ComponentFactory.Create<Spinner>(scene.Manager.Find("A Pyramid"));

                // get a snapshot of contents in component manager (breakpoint following line)
                //System.Diagnostics.Debug.WriteLine(scene.Manager.Count);
            }

            ksLast = Keyboard.GetState();

            base.Update(gameTime);
        }
    }
}
